import axios from "./http";
import "../utils/conversion";
import {
  formatterStateStationRealtime,
  formatterWeather,
  formatterProvincialStationRealtime,
  formatterStationHourlyStatistics,
  formatterSummaryStatistics,
  formatterVelocity
} from "../utils/conversion";

const lc_port_10085 = "http://47.92.107.19:10085/";
const lc_port_12016 = "http://47.92.107.19:12016/";

const params = {
  districtId: 2,
  cityDistrict: 2,
  cooperateDistrict: 3,
  stateStation: 11,
  province: "河北省",
  cityId: "1742",
  city: "石家庄市"
};

const getDistrictByCondition = (params, callback) => {
  axios
    .post(lc_port_10085 + "district/findDistrictByCondition", params)
    .then(res => {
      callback(res.data.success ? res.data.data : {});
    });
};

const getDistrictListByCondition = (params, callback) => {
  axios
    .post(lc_port_10085 + "district/findDistrictListByCondition", params)
    .then(res => {
      callback(res.data.success ? res.data.data : []);
    });
};

const getStateStationRealtimeByCondition = (params, callback) => {
  axios
    .all([
      axios.get(
        "http://nairp.market.alicloudapi.com/api/v1/nation_air_by_province/station_realtime_list",
        {
          params,
          headers: {
            Authorization: "APPCODE c0079e076f774abd8b82dbbaf30e0eb6"
          }
        }
      ),
      axios.post(lc_port_10085 + "device/findDeviceListByCondition", params)
    ])
    .then(res => {
      let array = formatterStateStationRealtime(res[0].data, res[1].data);
      callback(array);
    });
};

const getWeather = (params, callback) => {
  axios
    .all([
      axios.post(
        "http://aliv13.data.moji.com/whapi/json/alicityweather/aqi",
        {
          cityId: params.cityId,
          token: "8b36edf8e3444047812be3a59d27bab9"
        },
        {
          headers: {
            Authorization: "APPCODE c0079e076f774abd8b82dbbaf30e0eb6"
          }
        }
      ),
      axios.post(
        "http://aliv13.data.moji.com/whapi/json/alicityweather/condition",
        {
          cityId: params.cityId,
          token: "50b53ff8dd7d9fa320d3d3ca32cf8ed1"
        },
        {
          headers: {
            Authorization: "APPCODE c0079e076f774abd8b82dbbaf30e0eb6"
          }
        }
      )
    ])
    .then(res => {
      let weather = formatterWeather(res[0].data, res[1].data);
      callback(weather);
    });
};

const getProvinceStationRealtimeByCondition = (params, callback) => {
  axios
    .get(
      "http://cchinair.market.alicloudapi.com/api/v1/air_all/city_station_realtime",
      {
        params,
        headers: {
          Authorization: "APPCODE c0079e076f774abd8b82dbbaf30e0eb6"
        }
      }
    )
    .then(res => {
      let array = formatterProvincialStationRealtime(res.data);
      callback(array);
    });
};

const getStationHourlyStatisticsByCondition = (params, callback) => {
  axios
    .post(lc_port_10085 + "stats/findHourlyStatisticsListByCondition", params)
    .then(res => {
      let array = formatterStationHourlyStatistics(res.data);
      callback(array);
    });
};

const getSummaryStatisticsListByCondition = (params, callback) => {
  axios
    .post(lc_port_10085 + "stats/findSummaryStatisticsListByCondition", params)
    .then(res => {
      let array = formatterSummaryStatistics(res.data);
      callback(array);
    });
};

const getVelocityByCondition = (params, callback) => {
  axios.post(lc_port_10085 + "velocity/findVelocityData", params).then(res => {
    let array = formatterVelocity(res.data);
    callback(array);
  });
};

const getNewTaskListByCondition = (params, callback) => {
  axios
    .get(lc_port_12016 + "task/findTaskAll", {
      params: params
    })
    .then(res => {
      callback(res.data.errcode - 0 === 0 ? res.data.data : []);
    });
};

const getTaskTypeListByCondition = callback => {
  axios.post(lc_port_12016 + "spot/getContaminateType").then(res => {
    callback(res.data.errcode - 0 === 0 ? res.data.data : []);
  });
};

export function verificationTask(params, callback) {
  axios.post(lc_port_12016 + "task/removeTask", params).then(res => {
    callback(res.data.errcode - 0 === 0 ? res.data.data : []);
  });
}

// const getVehicleListByCondition = callback => {
//   axios.post(lc_port_12014 + "collection/findCarcollectionAll").then(res => {
//     callback(res.data.errcode - 0 === 0 ? res.data.data : []);
//   });
// };
const getVehicleListByCondition = (params, callback) => {
  axios
    .get(lc_port_12016 + "CarOrUser/getCarOrUserOrSpotMessage", {
      params: params
    })
    .then(res => {
      callback(res.data.errcode - 0 === 0 ? res.data.data : []);
    });
};

export function getCityDistrict(callback) {
  getDistrictByCondition(
    {
      districtId: params.cityDistrict,
      type: params.cityDistrict
    },
    callback
  );
}

export function getStateStationRealtime(callback) {
  getStateStationRealtimeByCondition(
    {
      districtId: params.cityDistrict,
      type: params.stateStation,
      province: params.province
    },
    callback
  );
}

export function getConversionOfWeatherData(callback) {
  getWeather(
    {
      cityId: params.cityId
    },
    callback
  );
}

export function getProvinceStationRealtime(callback) {
  getProvinceStationRealtimeByCondition(
    {
      city: params.city
    },
    callback
  );
}

export function getStationHourlyStatistics(callback) {
  getStationHourlyStatisticsByCondition(
    {
      districtId: params.districtId
    },
    callback
  );
}

export function getStationSummaryStatistics(districtId, callback) {
  getSummaryStatisticsListByCondition(
    {
      districtId: districtId
    },
    callback
  );
}

export function getCooperateDistrictList(callback) {
  getDistrictListByCondition(
    {
      districtId: params.districtId,
      type: params.cooperateDistrict
    },
    callback
  );
}

export function getCooperateDistrictVelocity(
  { districtId = params.districtId },
  callback
) {
  getVelocityByCondition(
    {
      districtId: districtId,
      type: params.cooperateDistrict
    },
    callback
  );
}

export function geCityDistrictVelocity(
  { districtId = params.districtId },
  callback
) {
  getVelocityByCondition(
    {
      districtId: districtId,
      type: params.cityDistrict
    },
    callback
  );
}

export function getNewTaskList(callback) {
  getNewTaskListByCondition(
    {
      areaCode: params.districtId
    },
    callback
  );
}

export function getTaskTypeList(callback) {
  getTaskTypeListByCondition(callback);
}

export function getVehicleList(callback) {
  getVehicleListByCondition(
    {
      areaCode: params.districtId
    },
    callback
  );
}

export function getSatelliteCloudPicture() {
  let satelliteCloudPicture = {};
  satelliteCloudPicture.index = 0;
  //获取整点前18小时以内的卫星云图（FY2G增强图）url
  satelliteCloudPicture.urlArray = [];
  let date = new Date();
  //获取当前时间的时间戳
  let time1 = Date.parse(date);
  //当前时间减去18个小时
  date.setHours(date.getHours() - 8);
  //获取18小时前时间戳
  let time2 = Date.parse(date);
  let flag = 1;
  while (flag) {
    time1 = time1 - 1000 * 60 * 60; //每一小时执行一次
    if (time1 < time2) {
      flag = 0;
    } else {
      let date2 = new Date(time1);
      let hour = date2.getUTCHours().toString(); //utc时
      let days = date2.getUTCDate().toString(); //utc天
      let month = (date2.getUTCMonth() + 1).toString();
      let years = date2.getUTCFullYear().toString();
      if (hour.length === 1) {
        hour = "0" + hour;
      }
      if (days.length === 1) {
        days = "0" + days;
      }
      if (month.length === 1) {
        month = "0" + month;
      }
      //除去17时和18时的
      let str =
        "http://image.nmc.cn/product/" +
        years +
        "/" +
        month +
        "/" +
        days +
        "/WXCL/medium/SEVP_NSMC_WXCL_ASC_E99_ACHN_LNO_PY_" +
        years +
        month +
        days +
        hour +
        "1500000.JPG";
      satelliteCloudPicture.urlArray.push(str);
    }
  }
  satelliteCloudPicture.url =
    satelliteCloudPicture.urlArray[satelliteCloudPicture.index];
  return satelliteCloudPicture;
}

export function technovoLidarLogin(callback) {
  axios
    .post("http://www.webkczg.com:8762/auth/login", {
      userCode: "sjz",
      password: "sjz"
    })
    .then(res => {
      callback(res.data);
    });
}
